/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model.provider;

import java.util.ArrayList;
import java.util.List;

import com.siro.pm.I18nManager;
import com.siro.pm.model.DemandOrBug;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Document;
import com.siro.pm.model.Project;
import com.siro.pm.utils.UUIDGenerator;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since ProjectProvider.java v1.0
 */
public class ProjectProvider {
	public static String PROJECT_PROPERTY_PROJECTNAME = I18nManager
			.getMessage("project.name");
	public static String PROJECT_PROPERTY_PROJECTMANAGER = I18nManager
			.getMessage("project.manager");
	public static String PROJECT_PROPERTY_PARTNERLEADER = I18nManager
			.getMessage("project.partnerleader");
	public static String PROJECT_PROPERTY_STATUS = I18nManager
			.getMessage("project.status");
	public static String PROJECT_PROPERTY_PROCESS = I18nManager
			.getMessage("project.process");
	public static String PROJECT_PROPERTY_STARTTIME = I18nManager
			.getMessage("project.starttime");
	public static String PROJECT_PROPERTY_ESTIMATEENDTIME = I18nManager
			.getMessage("project.endtime");
	
	public static String PROJECT_PROPERTY_DEVELOPERS = I18nManager
			.getMessage("project.developers");
	public static String PROJECT_PROPERTY_DOCUMENTS = I18nManager
			.getMessage("project.documents");
	public static String PROJECT_PROPERTY_BUGS = I18nManager
			.getMessage("project.bugs");

	public static String[] PROJECT_STATUS_DATA = {
			I18nManager.getMessage("project.status.inprocess"),
			I18nManager.getMessage("project.status.closed"),
			I18nManager.getMessage("project.status.complete"), };

	public static String[] PROJECT_PROCESS_DATA = {
			I18nManager.getMessage("project.process.approval"),
			I18nManager.getMessage("project.process.analysis"),
			I18nManager.getMessage("project.process.design"),
			I18nManager.getMessage("project.process.coding"),
			I18nManager.getMessage("project.process.testing"),
			I18nManager.getMessage("project.process.running"),

	};
	static IndexedContainer projectShortCutContainer;
	static IndexedContainer projectContainer;

	static {
		projectShortCutContainer = new IndexedContainer();
		projectShortCutContainer.addContainerProperty(
				PROJECT_PROPERTY_PROJECTNAME, String.class, "");
		projectShortCutContainer.addContainerProperty(
				PROJECT_PROPERTY_PROJECTMANAGER, String.class, "");
		// projectShortCutContainer.addContainerProperty(
		// PROJECT_PROPERTY_PARTNERLEADER, String.class, "");
		projectShortCutContainer.addContainerProperty(PROJECT_PROPERTY_STATUS,
				String.class, "");
		projectShortCutContainer.addContainerProperty(PROJECT_PROPERTY_PROCESS,
				String.class, "");
		projectShortCutContainer.addContainerProperty(
				PROJECT_PROPERTY_STARTTIME, String.class, "");
		projectShortCutContainer.addContainerProperty(
				PROJECT_PROPERTY_ESTIMATEENDTIME, String.class, "");

		projectContainer = new IndexedContainer();
		projectContainer.addContainerProperty(PROJECT_PROPERTY_PROJECTNAME,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_PROJECTMANAGER,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_PARTNERLEADER,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_STATUS,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_PROCESS,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_STARTTIME,
				String.class, "");
		projectContainer.addContainerProperty(PROJECT_PROPERTY_ESTIMATEENDTIME,
				String.class, "");

	}

	public static IndexedContainer getProjectShortCutData(Developer developer) {
		projectShortCutContainer.removeAllItems();
		List<Project> projects = getProjectsData(developer);
		for (Project project : projects) {
			Item item = projectShortCutContainer.addItem(project);
			item.getItemProperty(PROJECT_PROPERTY_PROJECTNAME).setValue(
					project.getName());
			item.getItemProperty(PROJECT_PROPERTY_PROJECTMANAGER).setValue(
					project.getManager().getName());
			// item.getItemProperty(PROJECT_PROPERTY_PARTNERLEADER).setValue(
			// project.getPartnerLeader().getName());
			item.getItemProperty(PROJECT_PROPERTY_STATUS).setValue(
					project.getStatus());
			item.getItemProperty(PROJECT_PROPERTY_PROCESS).setValue(
					project.getProcess());
			item.getItemProperty(PROJECT_PROPERTY_STARTTIME).setValue(
					project.getStartTime());
			item.getItemProperty(PROJECT_PROPERTY_ESTIMATEENDTIME).setValue(
					project.getEstimatedEndTime());
		}
		return projectShortCutContainer;
	}

	public static IndexedContainer getProjectData(Developer developer) {
		projectContainer.removeAllItems();
		List<Project> projects = getProjectsData(developer);
		for (Project project : projects) {
			Item item = projectContainer.addItem(project);
			item.getItemProperty(PROJECT_PROPERTY_PROJECTNAME).setValue(
					project.getName());
			item.getItemProperty(PROJECT_PROPERTY_PROJECTMANAGER).setValue(
					project.getManager().getName());
			item.getItemProperty(PROJECT_PROPERTY_PARTNERLEADER).setValue(
					project.getPartnerLeader().getName());
			item.getItemProperty(PROJECT_PROPERTY_STATUS).setValue(
					project.getStatus());
			item.getItemProperty(PROJECT_PROPERTY_PROCESS).setValue(
					project.getProcess());
			item.getItemProperty(PROJECT_PROPERTY_STARTTIME).setValue(
					project.getStartTime());
			item.getItemProperty(PROJECT_PROPERTY_ESTIMATEENDTIME).setValue(
					project.getEstimatedEndTime());
		}
		return projectContainer;
	}

	// TODO: Delete
	// static List<Project> projects = new ArrayList<Project>();

	public static List<Project> getProjectsData(Developer developer) {
		if (developer == null) {
			return Project.getAllProjects();
		} else {
			return Project.getProjects(developer);
		}
		// TODO: 取得用户相关的工程信息
		// if (projects.size() > 0)
		// return projects;
		// for (int i = 0; i < 10; i++) {
		// Project project = new Project();
		// project.setId(UUIDGenerator.newUUID());
		// project.setName("Test Project" + i);
		// Developer developer = new Developer();
		// developer.setId(UUIDGenerator.newUUID());
		// developer.setName("Macheal");
		// project.setManager(developer);
		// developer = new Developer();
		// developer.setId(UUIDGenerator.newUUID());
		// developer.setName("Kobe Brant");
		// project.setPartnerLeader(developer);
		// project.setStatus("In Process");
		// project.setProcess("50%");
		// project.setStartTime("2012-08-22");
		// project.setEstimatedEndTime("2012-12-12");
		// project.setDevelopers(getDevelopersData(project));
		// project.setDocs(getDocumentsData(project));
		// project.setDemandsOrBugs(getDebugsData(project));
		// projects.add(project);
		// }
		// return projects;
	}

	// TODO:Delete
	public static List<Developer> getDevelopersData(Project project) {
		List<Developer> developers = new ArrayList<Developer>();
		for (int i = 0; i < 50; i++) {
			Developer developer = new Developer();
			developer.setId(UUIDGenerator.newUUID());
			developer.setName("Test Developer");
			developer.setMailAddr("Test@test.com");
			developer.setOrgName("Test Org");
			developer.setPassword("******");
			developer.setPhoneNum("86-010-12345678");
			developer.setPostName("Enginere");
			developer.setRole("Developer");
			developers.add(developer);
		}
		return developers;
	}

	// TODO:Delete
	public static List<Document> getDocumentsData(Project project) {
		List<Document> documents = new ArrayList<Document>();
		for (int i = 0; i < 50; i++) {
			Document document = new Document();
			document.setId(UUIDGenerator.newUUID());
			document.setName("Test Developer");
			document.setPath("/docs/Test_Project8/操作流程.doc");
			document.setProject(project);
			documents.add(document);
		}
		return documents;
	}

	// TODO:Delete
	public static List<DemandOrBug> getDebugsData(Project project) {
		List<DemandOrBug> bugs = new ArrayList<DemandOrBug>();
		for (int i = 0; i < 50; i++) {
			DemandOrBug bug = new DemandOrBug();
			bug.setId(UUIDGenerator.newUUID());
			bug.setName("Test Bug");
			bug.setDesc("This is a test bug.");
			bug.setProject(project);
			bug.setLevel("nomal");
			Developer developer = new Developer();
			developer.setId(UUIDGenerator.newUUID());
			developer.setName("developer1");
			bug.setCreator(developer);
			bug.setSolver(developer);
			bug.setAdvisor(developer);
			bug.setStatus("In Process");
			bug.setResult("Result");
			bug.setCreatetime("2012-08-22");
			bug.setClosetime("2012-12-12");
			bugs.add(bug);
		}
		return bugs;
	}
}
